﻿$(document).ready(function () {
    $("#grid").flexigrid({
        url: 'InsereGrid.aspx/getDados',
        dataType: 'json',
        colModel: [
            { display: 'teste', name: 'teste', width: 40, sortable: true, align: 'center' },
            { display: 'teste2', name: 'teste2', width: 180, sortable: true, align: 'left' },
            { display: 'teste3', name: 'teste3', width: 140, sortable: true, align: 'center' },
            { display: 'teste4', name: 'teste4', width: 100, sortable: true, align: 'right' },
            { display: 'teste5', name: 'teste5', width: 100, sortable: true, align: 'center'/*, hide: true*/ }
        ],
        buttons: [
            { name: 'Adicionar', bclass: 'add', onpress: addToTable },
            { name: 'Editar', bclass: 'edit', onpress: editTable },
            { name: 'Remover', bclass: 'delete', onpress: deleteFromTable }
        ],
        searchitems: [
            { display: 'teste', name: 'teste' },
            { display: 'teste2', name: 'teste2', isdefault: true }
        ],
        sortname: "teste",
        sortorder: "asc",
        usepager: true,
        title: 'Titulo Glorioso',
        useRp: true,
        rp: 15,
        showTableToggleBtn: true,
        width: 700,
        height: 400
    });
});

function addToTable() {
    var registro = "teste|lindo|de|deus";
    $.ajax({
        type: 'POST',
        url: 'InsereGrid.aspx/addToTable',
        data: "{dados:'"+registro + "'}",
        dataType: 'json',
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            $("#grid").flexReload();
        }
    });
}

function editTable() {
    var id = 100000;
    var registro = "teste|editando|lindo|de deus";
    $.ajax({
        type: 'POST',
        url: 'InsereGrid.aspx/editTable',
        data: "{id:" +id+ ", dados:'" + registro + "'}",
        dataType: 'json',
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            $("#grid").flexReload();
        }
    });
}

function deleteFromTable() {
    var id = 1;
    $.ajax({
        type: 'POST',
        url: 'InsereGrid.aspx/deleteFromTable',
        data: "{id:" + id + "}",
        dataType: 'json',
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            $("#grid").flexReload();
        }
    });
}